﻿using Model.CommonEntities;
using Model.StrategyEntities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BLL
{
    public sealed partial class AnalysisEngine
    {
        /// <summary>
        /// 捕捞季节
        /// VAR1:=(2*CLOSE+HIGH+LOW)/3;
        /// VAR2:= EMA(EMA(EMA(VAR1, 3), 3), 3);
        /// J: (VAR2 - REF(VAR2, 1)) / REF(VAR2, 1) * 100;
        /// D: MA(J, 2);
        /// K: MA(J, 1);
        /// </summary>
        /// <param name="stocklist"></param>
        /// <param name="period"></param>
        /// <returns></returns>
        public static BLJJResult BLJJ(SortedList<string, SingleStockStru> stocklist, int period = 30)
        {
            List<decimal> testValues = new List<decimal>();
            //时间列表
            List<decimal> TList = new List<decimal>();
            //收盘价列表
            List<decimal> CloseList = new List<decimal>();
            //开盘价列表
            List<decimal> OpenList = new List<decimal>();
            //最高价列表
            List<decimal> HighList = new List<decimal>();
            //最低价列表
            List<decimal> LowList = new List<decimal>();
            //买入基准价
            List<decimal> arg = new List<decimal>();
            //(2*CLOSE+HIGH+LOW)/3
            foreach (var item in stocklist)
            {
                testValues.Add((2 * item.Value.close + item.Value.high + item.Value.low) / 3);
                //testValues.Add((4 * item.Value.close) / 3);
                //testValues.Add((4 * item.Value.open) / 3);
            }

            //J: (VAR2 - REF(VAR2, 1)) / REF(VAR2, 1) * 100
            List<decimal> pre_ema_list = AnalysisEngine.EMA(AnalysisEngine.EMA(AnalysisEngine.EMA(testValues, 3).Values, 3).Values, 3).Values;
            List<decimal> DList = new List<decimal>();
            List<decimal> KList = new List<decimal>();
            List<List<decimal>> Result = new List<List<decimal>>();
            //根据给定显示周期period，逆时序截取period条数据：D、K线，Time，开盘价，收盘价，最高价，最低价
            if (pre_ema_list.Count > period)
            {
                for (int i = 0; i < Convert.ToInt32(period); i++)
                {
                    decimal J1 = (AnalysisEngine.REF(pre_ema_list, i) - AnalysisEngine.REF(pre_ema_list, i + 1)) / AnalysisEngine.REF(pre_ema_list, i + 1) * 100;
                    decimal J2 = (AnalysisEngine.REF(pre_ema_list, i + 1) - AnalysisEngine.REF(pre_ema_list, i + 2)) / AnalysisEngine.REF(pre_ema_list, i + 2) * 100;
                    DList.Add((J1 + J2) / 2);
                    KList.Add(J1);
                    TList.Add(Convert.ToDecimal(stocklist.Keys.ToList<string>()[stocklist.Count - i - 1]));
                    CloseList.Add(stocklist[Convert.ToString(stocklist.Keys.ToList<string>()[stocklist.Count - i - 1])].close);
                    OpenList.Add(stocklist[Convert.ToString(stocklist.Keys.ToList<string>()[stocklist.Count - i - 1])].open);
                    HighList.Add(stocklist[Convert.ToString(stocklist.Keys.ToList<string>()[stocklist.Count - i - 1])].high);
                    LowList.Add(stocklist[Convert.ToString(stocklist.Keys.ToList<string>()[stocklist.Count - i - 1])].low);
                }
            }
            else
            {
                for (int i = 0; i < pre_ema_list.Count; i++)
                {
                    decimal J1 = (AnalysisEngine.REF(pre_ema_list, i) - AnalysisEngine.REF(pre_ema_list, i + 1)) / AnalysisEngine.REF(pre_ema_list, i + 1) * 100;
                    decimal J2 = (AnalysisEngine.REF(pre_ema_list, i + 1) - AnalysisEngine.REF(pre_ema_list, i + 2)) / AnalysisEngine.REF(pre_ema_list, i + 2) * 100;
                    DList.Add(J1 + J2);
                    KList.Add(J1);
                    TList.Add(Convert.ToDecimal(stocklist.Keys.ToList<string>()[stocklist.Count - i - 1]));
                    CloseList.Add(stocklist[Convert.ToString(stocklist.Keys.ToList<string>()[stocklist.Count - i - 1])].close);
                    OpenList.Add(stocklist[Convert.ToString(stocklist.Keys.ToList<string>()[stocklist.Count - i - 1])].open);
                    HighList.Add(stocklist[Convert.ToString(stocklist.Keys.ToList<string>()[stocklist.Count - i - 1])].high);
                    LowList.Add(stocklist[Convert.ToString(stocklist.Keys.ToList<string>()[stocklist.Count - i - 1])].low);
                }
            }
            DList.Reverse();
            KList.Reverse();
            TList.Reverse();
            CloseList.Reverse();
            OpenList.Reverse();
            HighList.Reverse();
            LowList.Reverse();

            //基准价取开盘价和收盘价的中值
            for (int i = 0; i < CloseList.Count; i++)
            {
                arg.Add((CloseList[i] + OpenList[i]) / 2);
            }
            var result = new BLJJResult()
            {
                StockCode = stocklist.First().Value.code,
                LongList = DList,
                ShortList = KList,
                TList = TList,
                CloseList = CloseList,
                OpenList = OpenList,
                HighList = HighList,
                LowList = LowList,
                BenchmarkList = CloseList
            };

            return result;

        }
    }
}
